﻿@model StudentEditViewModel
@inject IStudentRepository _students

@{
    ViewBag.Title = "编辑学生";
    var photoSrc = Model.ExistedPhotoPath == null ? "~/images/nophoto.jpg" : $"~/uploads/images/{Model.ExistedPhotoPath}";
}

<form enctype="multipart/form-data" asp-controller="student" asp-action="edit" method="post" class="mt-3">

    <input hidden asp-for="Id" />
    <input hidden asp-for="ExistedPhotoPath" />

    <div class="text-danger" asp-validation-summary="All"></div>

    <div class="form-group row">
        <label asp-for="Name" class="col-sm-2 col-form-label"></label>
        <div class="col-sm-10">
            <input asp-for="Name" class="form-control" placeholder="请输入名称" />
            <span class="text-danger" asp-validation-for="Name"></span>
        </div>
    </div>

    <div class="form-group row">
        <label asp-for="Email" class="col-sm-2 col-form-label"></label>
        <div class="col-sm-10">
            <input asp-for="Email" class="form-control" placeholder="请输入邮箱" />
            <span class="text-danger" asp-validation-for="Email"></span>
        </div>
    </div>

    <div class="form-group row">
        <label asp-for="ClassName" class="col-sm-2 col-form-label"></label>
        <div class="col-sm-10">
            <select asp-for="ClassName" asp-items="Html.GetEnumSelectList<ClassNameEnum>()" class="custom-select mr-sm-2">
                <option value="">请选择</option>
            </select>
            <span class="text-danger" asp-validation-for="ClassName"></span>
        </div>
    </div>

    <div class="form-group row">
        <label asp-for="Photo" class="col-sm-2 col-form-label"></label>
        <div class="col-sm-10">
            <div class="custom-file">
                <input asp-for="Photo" class="form-control custom-file-input" />
                <label class="custom-file-label">请选择图片</label>
            </div>
        </div>
    </div>

    <div class="form-group row col-sm-4 offset-4">
        <img src="@photoSrc" class="image-thumbnail" alt="Alternate Text" asp-append-version="true" />
    </div>

    <div class="form-group row">
        <div class="col-sm-10">
            <button type="submit" class="btn btn-primary">保存</button>
            <a asp-controller="student" asp-action="index" class="btn btn-secondary">取消</a>
        </div>
    </div>

    <div class="form-group row">
        <div class="col-sm-10">
            学生总人数：@_students.GetAll().Count().ToString()
        </div>
    </div>

    @section Scripts{
        <script type="text/javascript">
            $(document).ready(function () {
                $('.custom-file-input').on('change', function () {
                    var fileName = $(this).val().split('\\').pop();
                    $(this).next('.custom-file-label').html(fileName);
                });
            });
        </script>
    }
</form>